package org.lc.stk.web.controller.baseInfo;

import java.util.List;
import java.util.stream.Collectors;

import org.lc.stk.model.baseInfo.MajorCengci;
import org.lc.stk.service.baseInfo.MajorCengciService;
import org.lc.stk.web.dto.majorcengci.CreateMajorCengciRequest;
import org.lc.stk.web.dto.majorcengci.MajorCengciQuery;
import org.lc.stk.web.dto.majorcengci.MajorCengciResponse;
import org.lc.stk.web.dto.majorcengci.UpdateMajorCengciRequest;
import org.lc.stk.web.response.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;

@RestController
@RequestMapping("/api/v1/baseInfo/majorCengci")
@Tag(name = "专业培养层次关联管理", description = "专业与培养层次关联的增删改查接口")
@Validated
public class MajorCengciController {

    @Autowired
    private MajorCengciService majorCengciService;

    @PostMapping
    @Operation(summary = "创建专业培养层次关联")
    public ApiResponse<MajorCengciResponse> create(@Valid @RequestBody CreateMajorCengciRequest request) {
        MajorCengci majorCengci = majorCengciService.create(request);
        return ApiResponse.success(MajorCengciResponse.fromEntity(majorCengci));
    }

    @PutMapping
    @Operation(summary = "更新专业培养层次关联")
    public ApiResponse<MajorCengciResponse> update(@Valid @RequestBody UpdateMajorCengciRequest request) {
        MajorCengci majorCengci = majorCengciService.update(request);
        return ApiResponse.success(MajorCengciResponse.fromEntity(majorCengci));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除专业培养层次关联")
    public ApiResponse<Void> delete(
            @Parameter(description = "关联ID", required = true)
            @PathVariable Integer id) {
        majorCengciService.delete(id);
        return ApiResponse.success();
    }

    @DeleteMapping("/major/{majorId}")
    @Operation(summary = "批量删除专业的培养层次关联")
    public ApiResponse<Integer> deleteByMajorId(
            @Parameter(description = "专业ID", required = true)
            @PathVariable Integer majorId) {
        int count = majorCengciService.deleteByMajorId(majorId);
        return ApiResponse.success(count);
    }

    @DeleteMapping("/cengci/{cengciId}")
    @Operation(summary = "批量删除培养层次的专业关联")
    public ApiResponse<Integer> deleteByCengciId(
            @Parameter(description = "培养层次ID", required = true)
            @PathVariable Integer cengciId) {
        int count = majorCengciService.deleteByCengciId(cengciId);
        return ApiResponse.success(count);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取专业培养层次关联详情")
    public ApiResponse<MajorCengciResponse> getById(
            @Parameter(description = "关联ID", required = true)
            @PathVariable Integer id) {
        MajorCengci majorCengci = majorCengciService.getById(id);
        return ApiResponse.success(MajorCengciResponse.fromEntity(majorCengci));
    }

    @GetMapping("/major/{majorId}")
    @Operation(summary = "获取专业的所有培养层次关联")
    public ApiResponse<List<MajorCengciResponse>> getByMajorId(
            @Parameter(description = "专业ID", required = true)
            @PathVariable Integer majorId) {
        List<MajorCengci> list = majorCengciService.getByMajorId(majorId);
        List<MajorCengciResponse> responseList = list.stream()
                .map(MajorCengciResponse::fromEntity)
                .collect(Collectors.toList());
        return ApiResponse.success(responseList);
    }

    @GetMapping("/cengci/{cengciId}")
    @Operation(summary = "获取培养层次的所有专业关联")
    public ApiResponse<List<MajorCengciResponse>> getByCengciId(
            @Parameter(description = "培养层次ID", required = true)
            @PathVariable Integer cengciId) {
        List<MajorCengci> list = majorCengciService.getByCengciId(cengciId);
        List<MajorCengciResponse> responseList = list.stream()
                .map(MajorCengciResponse::fromEntity)
                .collect(Collectors.toList());
        return ApiResponse.success(responseList);
    }

    @GetMapping
    @Operation(summary = "分页查询专业培养层次关联")
    public ApiResponse<List<MajorCengciResponse>> query(
            @Parameter(description = "查询条件")
            MajorCengciQuery query,
            @Parameter(description = "页码(从0开始)", example = "0")
            @RequestParam(defaultValue = "0") int page,
            @Parameter(description = "每页大小", example = "10")
            @RequestParam(defaultValue = "10") int size) {
        
        Sort sort = Sort.by(
            query.getSortDirection().equalsIgnoreCase("desc") ? 
            Sort.Direction.DESC : Sort.Direction.ASC, 
            query.getSortBy()
        );
        
        Page<MajorCengci> resultPage = majorCengciService.query(query, PageRequest.of(page, size, sort));
        
        List<MajorCengciResponse> responseList = resultPage.getContent().stream()
                .map(MajorCengciResponse::fromEntity)
                .collect(Collectors.toList());
        
        return ApiResponse.success(
            responseList,
            new ApiResponse.PageInfo(
                resultPage.getNumber(),
                resultPage.getSize(),
                resultPage.getTotalElements(),
                resultPage.getTotalPages()
            )
        );
    }

    @GetMapping("/exists")
    @Operation(summary = "检查专业和培养层次的关联是否已存在")
    public ApiResponse<Boolean> checkExists(
            @Parameter(description = "专业ID", required = true)
            @RequestParam Integer majorId,
            @Parameter(description = "培养层次ID", required = true)
            @RequestParam Integer cengciId,
            @Parameter(description = "排除的关联ID")
            @RequestParam(required = false) Integer excludeId) {
        return ApiResponse.success(majorCengciService.exists(majorId, cengciId, excludeId));
    }
}